package com.groupbuying.customerconsumer.mq;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.groupbuying.customerconsumer.utils.JSONUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付回执统一处理监听
 *
 * @Author Vincentxin
 * @Date 2019-05-29
 */
@Component
public class CustomerListener implements MessageListener {
    public static final Logger LOGGER = LoggerFactory.getLogger(CustomerListener.class);

    @Autowired
    private CustomerWechat customerWechat;

    @Autowired
    private CustomerAlipay customerAlipay;

    @Override
    public Action consume(Message message, ConsumeContext context) {
        //Topic校验Tag校验
        if (!message.getTopic().equals(CustomerMQConfig.TOPIC) || StringUtils.isEmpty(message.getTag())) {
            LOGGER.error("Topic不符合订阅要求，Topic：" + message.getTopic() + "或者Tag为空，Tag：" + message.getTag());
            return Action.CommitMessage;
        }
        //获取消息重试次数
        int reconsume = message.getReconsumeTimes();
        if (reconsume == 3) {
            LOGGER.error("该消息重复消费三次，直接标识成功" + message.getTag());
            return Action.CommitMessage;

        }
        //解析消息体
        Map<String, Object> result = new HashMap<>();
        try {
            result = JSONUtils.jsonToMap(new String(message.getBody()));
        } catch (Exception e) {
            LOGGER.error("消息体解析错误，错误信息:{}", e.getMessage());
            e.printStackTrace();
        }

        LOGGER.info("解析后的消息体获取消息：" + result.toString());

        boolean handleResult = true;
        /**
         * 通过TAG来判断处理方式
         * 备注：此处提现多态，可以使用策略来处理。
         */
        if (CustomerMQConfig.TAG_CUSTOMER_PHONE_NEW.equals(message.getTag())) {
            //新增用户信息处理
            LOGGER.info("===========微信新增用户信息处理：============");
            handleResult = customerWechat.saveObject(result);
        } else if (CustomerMQConfig.TAG_CUSTOMER_PHONE_UPDATE.equals(message.getTag())) {
            //更新用户信息处理
            LOGGER.info("===========微信修改用户信息处理：============");
            customerWechat.updateObject(result);
        } else if (CustomerMQConfig.TAG_CUSTOMER_WX_REGISTER_NEW.equals(message.getTag())) {
            //优惠买单用，微信注册新增用户
            LOGGER.info("===========优惠买单用，微信注册新增用户信息处理：============");
            handleResult = customerWechat.saveWXRegisterObject(result);
        } else if (CustomerMQConfig.TAG_CUSTOMER_ALIPAY_REGISTER_NEW.equals(message.getTag())) {
            //支付宝注册新增用户
            LOGGER.info("===========支付宝注册新增用户信息处理：============");
            handleResult = customerAlipay.saveAlipayRegisterObject(result);
        }

        //判断结果
        if (!handleResult) {
            LOGGER.error("处理失败；Tag：{}", message.getTag());


        }
        return Action.CommitMessage;

    }
}
